package com.jxd.reportsys.controller;

import com.jxd.reportsys.model.Dept;
import com.jxd.reportsys.model.Emp;
import com.jxd.reportsys.model.Report;
import com.jxd.reportsys.service.IDeptService;
import com.jxd.reportsys.service.IEmpService;
import com.jxd.reportsys.service.IReportService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName ReportController
 * @Description TODO
 * @Author 朱隶文
 * @Date 2024/9/22 11:18
 * @Version 1.0
 */
@RestController
public class ReportController {

    @Autowired
    private IReportService reportService;
    @Autowired
    private IEmpService empService;
    @Autowired
    private IDeptService deptService;

    @RequestMapping("/getDeptReportInfoSummary")
    public List<Map<String, String>> getDeptReportInfoSummary(@RequestBody Map<String, Object> map) {
        int deptno = (Integer) map.get("deptno");
        String reportdate = (String) map.get("reportdate");
        //调用方法获取部门待审核报工信息总结
        List<Map<String, Object>> infos = reportService.getDeptReportSummary(deptno, reportdate);
        //获取部门应报人数
        //只有入职日期在报工日期之前的才计数
        int empCount = empService.getEmpCountByDeptno(deptno, reportdate);
        List<Map<String, String>> summaryList = new ArrayList<>();
        for (int i = 0; i < infos.size(); i++) {
            Map<String, Object> info = infos.get(i);
            //将集合中的null替换为0
            if(info.get("unreviewedNum") == null) {
                info.put("unreviewedNum", 0);
            }
            if(info.get("auditedNum") == null) {
                info.put("auditedNum", 0);
            }
            if(info.get("returnNum") == null) {
                info.put("returnNum", 0);
            }
            //获取部门应报人数
            empCount = empService.getEmpCountByDeptno(deptno, info.get("reportdate") + "");
            String summary = "应报" + empCount + "人，已报" + info.get("reportNum") + "人，提交" + info.get("auditedNum") + "人，未审" + info.get("unreviewedNum") + "人（退回" + info.get("returnNum") + "人）";
            Map<String, String> summaryMap = new HashMap<>();
            summaryMap.put("date", (info.get("reportdate") + ""));
            summaryMap.put("summary", summary);
            summaryList.add(summaryMap);
        }
        if(infos.size() == 0 && reportdate != "") {
            String summary = "应报" + empCount + "人，已报" + 0 + "人，提交" + 0 + "人，未审" + 0 + "人（退回" + 0 + "人）";
            Map<String, String> summaryMap = new HashMap<>();
            summaryMap.put("date", reportdate);
            summaryMap.put("summary", summary);
            summaryList.add(summaryMap);
        }
        return summaryList;
    }

    @RequestMapping("/getReport")
    public Map<String, Object> getReport(@RequestBody Map<String, Object> map) {
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> reports = reportService.getReport(map);
        for (int i = 0; i < reports.size(); i++) {
            int attendance = (Integer) reports.get(i).get("attendance");
            if( attendance == 1) {
                reports.get(i).put("attendance", "上班");
            } else if (attendance == 2) {
                reports.get(i).put("attendance", "调休");
            } else {
                reports.get(i).put("attendance", "请假");
            }
        }
        data.put("reports", reports);
        int count = reportService.getCount(map);
        data.put("count", count);
        return data;
    }

    @RequestMapping("/auditReport")
    public String auditReport(@RequestBody Map<String, Object> map) {
        String auditStr = "error";
        boolean auditFlag = reportService.auditReport(map);
        if(auditFlag) {
            auditStr = "success";
        }
        return auditStr;
    }

    @RequestMapping("/auditReportByBatch")
    public String auditReportByBatch(@RequestBody Map<String, Object> map) {
        String auditStr = "error";
        boolean auditFlag = reportService.auditReportByBatch(map);
        if(auditFlag) {
            auditStr = "success";
        }
        return auditStr;
    }

    @RequestMapping("/getAllFailReportdate")
    public  List<Map<String, Object>> getAllFailReportdate(@RequestBody Map<String, Object> map) {
        return reportService.getAllFailReportdate(map);
    }

    @RequestMapping("/getAllReportdate")
    public List<String> getAllReportdate(@RequestBody Map<String, Object> map) {
        return reportService.getAllReportdate((Integer) map.get("empno"), map.get("hiredate") + "");
    }

    @RequestMapping("/getReportByEmpnoAndReportdate")
    public Report getReportByEmpnoAndReportdate(@RequestBody Map<String, Object> map) {
        int empno = (Integer) map.get("empno");
        String reportdate = map.get("reportdate") + "";
        return reportService.getReportByEmpnoAndReportdate(empno, reportdate);
    }

    @RequestMapping("/empReport")
    public String empReport(@RequestBody Report report) {
        int empno = report.getEmpno();
        String reportdate = report.getReportdate();
        //调用方法查询是否存在对应报工信息
        Report report1 = reportService.getReportByEmpnoAndReportdate(empno, reportdate);
        boolean reportFlag = false;
        if(report1 == null) {
            //不存在则新增报工
            reportFlag = reportService.addReport(report);
        } else {
            //存在则修改报工
            reportFlag = reportService.updateReport(report);
        }
        if(reportFlag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/getFailReport")
    public Map<String, Object> getFailReport(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        Map<String, Object> failReportInfoMap = new HashMap<>();
        List<Map<String, Object>> failReportInfo = new ArrayList<>();
        int page = (Integer)map.get("page");
        int limit = (Integer)map.get("limit");
        int skip = (page - 1) * limit;
        List<Emp> empList = empService.getEmpByDeptnoAndEname(map);
        System.out.println(empList);
        for (int i = 0; i < empList.size(); i++) {
            map.put("empno", empList.get(i).getEmpno());
            map.put("hiredate", empList.get(i).getHiredate());
            System.out.println(map);
            List<Map<String, Object>> list = reportService.getAllFailReportdate(map);
            for (int j = 0; j < list.size(); j++) {
                list.get(j).put("empno", empList.get(i).getEmpno());
                list.get(j).put("ename", empList.get(i).getEname());
                failReportInfo.add(list.get(j));
            }
        }
        failReportInfoMap.put("count", failReportInfo.size());
        if(failReportInfo.size() > skip) {
            if (skip > 0) {
                failReportInfo.subList(0, skip).clear();
            }
            if (failReportInfo.size() > limit) {
                failReportInfo.subList(limit, failReportInfo.size()).clear();
            }
        }
        failReportInfoMap.put("data", failReportInfo);
        return failReportInfoMap;
    }

}
